package com.zdd.ehe.controller;

import com.zdd.ehe.entity.Picture;
import com.zdd.ehe.result.Result;
import com.zdd.ehe.service.IPictureService;
import com.zdd.ehe.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @title:PictureUpload
 * @author:ZhuiZhu
 * @description:图片上传模块
 * @createDate: 2023/3/17  21:41
 */
@Api(tags = "图片上传模块")
@Controller
@RequestMapping("/picture")
public class PictureController {
    @Resource
    private IPictureService pictureService;


    @ApiOperation("图片上传接口")
    @PostMapping("/upload")
    @ResponseBody
    public Result multipleImageUpload(@RequestParam("picture") MultipartFile[] files,@RequestParam("user_id") Integer user_id,@RequestParam("post_id") Integer post_id,@RequestParam("comment_id") Integer comment_id){
        //System.out.println("上传的图片数："+files.length);
        //System.out.println("user_id: " + user_id + " post_id:" + post_id + " comment_id: " + comment_id);
        if(user_id == null || post_id == null && comment_id == null){
            return Result.error(4001,"缺失参数");
        }

        List<String> result= new ArrayList();//一个文件上传的结果

        for (MultipartFile file : files) {    //循环保存文件

            if (file.getSize() / 1024 / 1024 > 10){
                return Result.error(4002,"图片过大");
            }
            else{
                //判断上传文件格式
                String fileType = file.getContentType();
                if (fileType.equals("image/jpeg") || fileType.equals("image/png") || fileType.equals("image/jpeg")) {
                    // 要上传的目标文件存放的绝对路径
                    String realPath = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
                    final String localPath= realPath + "/img";
                    //System.out.println(localPath);
                    //上传后保存的文件名(需要防止图片重名导致的文件覆盖)
                    //获取文件名
                    String fileName = file.getOriginalFilename();
                    //获取文件后缀名
                    String suffixName = fileName.substring(fileName.lastIndexOf("."));
                    //重新生成文件名
                    fileName = UUID.randomUUID()+suffixName;
                    if (FileUtil.upload(file, localPath, fileName)) {
                        //文件存放的相对路径(一般存放在数据库用于img标签的src)
                        String relativePath="img/"+fileName;
                        String path = localPath + relativePath;
                        // 向数据库添加数据
                        Picture picture = new Picture();
                        picture.setDel("1");
                        picture.setCommentId(comment_id);
                        picture.setUserId(user_id);
                        picture.setPostId(post_id);
                        picture.setPath(path);
                        picture.setTitle(fileName);
                        picture.setPublishTime(LocalDateTime.now());
                        int flag = pictureService.insert(picture);
                        if(flag != 1){
                            return Result.error(4005,"数据保存失败");
                        }
                        result.add(relativePath);//前端根据是否存在该字段来判断上传是否成功
                    }
                    else{
                        return Result.error(4003,"图片保存失败");
                    }
                }
                else{
                    return Result.error(4004,"图片格式不正确");
                }
            }

        }
        return Result.success("上传成功",result);
    }



}
